using System;
using System.Collections.Generic;
using FluffyUnderware.DevTools.Extensions;
using JetBrains.Annotations;
using RuntimeInspectorNamespace;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace Yoozoo.Gameplay.City
{
    [Serializable]
    public class DecorationObject
    {
        public Mesh mesh;
        public Material[] materials;
        public int[] subMeshIndex;
        public bool[] enableInstancing;
        public bool castShadow;
        public Vector3 position;
        public Vector3 eulerAngle;
        public Vector3 scale;

        public void Collect(MeshFilter meshFilter)
        {
#if UNITY_EDITOR
            var mesh = meshFilter.sharedMesh;
            var meshRenderer = meshFilter.gameObject.GetComponent<MeshRenderer>();
            if (meshRenderer)
            {
                if (meshRenderer == null)
                {
                    return;
                }
            }
            
            this.mesh = mesh;
            materials = meshRenderer.sharedMaterials;
            enableInstancing = new bool[materials.Length];
            subMeshIndex = new int[materials.Length];
            castShadow = meshRenderer.shadowCastingMode != ShadowCastingMode.Off;
            position = meshFilter.transform.position;
            eulerAngle = meshFilter.transform.eulerAngles;
            scale = meshFilter.transform.lossyScale;
            for (int i = 0; i < materials.Length; i++)
            {
                enableInstancing[i] = materials[i].enableInstancing &&
                                      !DecorationMeshData.IsInBlackList(materials[i].shader.name);
                subMeshIndex[i] = Mathf.Min(i, mesh.subMeshCount - 1);
            }
            
            //检查mesh是否可读写
            /*var assetPath = AssetDatabase.GetAssetPath(mesh);
            var modelImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
            if (modelImporter)
            {
                if (!modelImporter.isReadable)
                {
                    modelImporter.isReadable = true;
                    modelImporter.SaveAndReimport();
                }
            }*/
#endif
        }
    }
    
    public class DecorationMeshData : MonoBehaviour
    {
        [SerializeField] public List<DecorationObject> objects = new List<DecorationObject>();
        private static HashSet<string> blackList = new HashSet<string>
        {
            "GGame/HRP/World/TreeTransparent",
        };

        public static bool IsInBlackList(string name)
        {
            return blackList.Contains(name);
        }

        
        public void AutoCollect()
        {
#if UNITY_EDITOR
            objects.Clear();
            var meshFilters = this.GetComponentsInChildren<MeshFilter>();
            for (int i = 0; i < meshFilters.Length; i++)
            {
                var data = new DecorationObject();
                data.Collect(meshFilters[i]);
                objects.Add(data);
            }
#endif
        }
    }
}